<?php
namespace app\controller;
use app\BaseController;
use app\model\Chat;
use app\model\User;
use app\Request;

class onlineChatController extends BaseController
{
    /**
     * 查询聊天信息
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function chat(Request $request)
    {
        //作为接收方，接收发送方发来的信息
        $uid = $request->user_id;       //获取用户id
        //查找出发送方id
        $from_user_id = Chat::where('to_user_id', '=', $uid)->value('from_user_id');
        //查找出user表的id，并找到他的名字
        $nameInfo=User::where('id','=',$from_user_id)->value('name');
        //需要查找的信息
        $dateArr=['text','log','create_time'];
        //查找chat表
        $getDate=Chat::where('from_user_id','=',$from_user_id)->where('to_user_id','=',$uid)->field($dateArr)->order('create_time desc')->select();
        //
        foreach ($getDate as $k => $v){
            $v['name'] = $nameInfo;
        }
        return $this->returnJson(self::SUCCESS,'asd',$getDate);
    }
    public function chat_look(Request $request){
        $uid = $request->user_id;
        $chat_id=Chat::where('to_user_id','=',$uid)->field('id')->select()->count();
        return $this->returnJson(self::SUCCESS,'asd',$chat_id);
    }
}